/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.webapp.add.ctrl;

import com.arg.deploy.bean.Tag;
import com.arg.deploy.webapp.common.DeployCheckboxRenderer;
import static com.arg.deploy.webapp.common.DeployRowRenderer.CENTER_CELL_STYLE;
import static com.arg.deploy.webapp.common.DeployRowRenderer.LABEL_STYLE;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Row;
import org.zkoss.zul.Textbox;

/**
 *
 * @author sorasaks
 */
public class AddTagHotFixRenderer extends DeployCheckboxRenderer {

    private static final Logger LOG = Logger.getLogger(AddTagHotFixRenderer.class);

    public AddTagHotFixRenderer(Checkbox chkAll, Grid grid) {
        setChkAll(chkAll);
        setGrid(grid);
    }

    @Override
    protected void gridRender(Row row, Object o, int i) throws Exception {
        refreshRenderer();
        Tag tag = (Tag) o;
        if (StringUtils.isEmpty(tag.getUsername())) {
            tag.setUsername(tag.getUser().getUsername());
        }

        if (tag.getIssue() == null
                && tag.getRevision() == null
                && tag.getPath() == null) {
            appendCellInRow(row, createCheckboxInCell(tag, LABEL_STYLE, row, i));
            appendCellInRow(row, createLabelInCell(String.valueOf(i + 1), CENTER_CELL_STYLE));
            appendCellInRow(row, createComboboxInCell(buildModeCombobox(tag.getMode()), LABEL_STYLE, Boolean.FALSE, new AutoComboboxSaveObject(tag, "mode")));
            appendCellInRow(row, createTextboxInCell(tag.getIssue(), LABEL_STYLE, Boolean.FALSE, new AutoTextboxSaveObject(tag, "issue")));
            appendCellInRow(row, createTextboxInCell(tag.getRevision(), LABEL_STYLE, Boolean.FALSE, new AutoTextboxSaveObject(tag, "revision")));
            appendCellInRow(row, createTextboxInCell(tag.getPath(), LABEL_STYLE, Boolean.FALSE, new AutoTextboxSaveObject(tag, "path")));
            appendCellInRow(row, createTextboxInCell(tag.getUsername(), LABEL_STYLE, Boolean.FALSE, new AutoTextboxSaveObject2(tag)));
        } else {
            appendCellInRow(row, createCheckboxInCell(tag, LABEL_STYLE, row, i));
            appendCellInRow(row, createLabelInCell(String.valueOf(i + 1), CENTER_CELL_STYLE));
            appendCellInRow(row, createComboboxInCell(buildModeCombobox(tag.getMode()), LABEL_STYLE, Boolean.TRUE, new AutoComboboxSaveObject(tag, "mode")));
            appendCellInRow(row, createTextboxInCell(tag.getIssue(), LABEL_STYLE, Boolean.TRUE, new AutoTextboxSaveObject(tag, "issue")));
            appendCellInRow(row, createTextboxInCell(tag.getRevision(), LABEL_STYLE, Boolean.TRUE, new AutoTextboxSaveObject(tag, "revision")));
            appendCellInRow(row, createTextboxInCell(tag.getPath(), LABEL_STYLE, Boolean.TRUE, new AutoTextboxSaveObject(tag, "path")));
            appendCellInRow(row, createTextboxInCell(tag.getUsername(), LABEL_STYLE, Boolean.TRUE, new AutoTextboxSaveObject2(tag)));
        }

        addListRow(row);
    }

//    public void remove() throws UnsupportedEncodingException {
//
//        Rows rows = grid.getRows();
//        List<Row> listRow = new ArrayList<Row>();
//        List<Checkbox> listCheck = new ArrayList<Checkbox>();
//
//        ListModelList listM = (ListModelList) grid.getModel();
//        List<Tag> list = listM.getInnerList();
//
//        if (rows != null
//                && getRowRenderer() != null
//                && !getRowRenderer().isEmpty()) {
//
//            Tag tagCloud = null;
//            Boolean isBreak = Boolean.FALSE;
//
//            Boolean isFinish = Boolean.FALSE;
//            while (isFinish == Boolean.FALSE) {
//                int idx = 0, countCheck = 0;
//                for (Row r : getRowRenderer()) {
//                    Checkbox chkbox = (Checkbox) r.getFirstChild().getFirstChild();
//                    Tag tag = (Tag) chkbox.getAttribute(EXECUTION_CHECKBOX_VALUE);
//                    if (chkbox.isChecked()) {
//                        ++countCheck;
//                        list.remove(idx);
//                        listCheck.add(chkbox);
//                        listRow.add(r);
//                        if (tag != null) {
//                            tagCloud = tag;
//                            Boolean isRemove = TagCore.remove(tag);
//                            if (!isRemove) {
//                                isBreak = Boolean.TRUE;
//                                break;
//                            }
//                        }
//                        break;
//                    }
//                    ++idx;
//                    if (countCheck == 0) {
//                        isFinish = Boolean.TRUE;
//                        break;
//                    }
//                }
//            }
//
//            if (tagCloud != null
//                    && isBreak) {
//                AlertMessages.alertMessage(ResourceUtil.getLabel("default.alert.message.failed.save", Locale.FRENCH));
//            }
//
//            for (Row r : listRow) {
//                super.row.remove(r);
//                rows.removeChild(r);
//            }
//
//            for (Checkbox chk : listCheck) {
//                listCheckbox.remove(chk);
//            }
//        }
//    }

    private static class AutoTextboxSaveObject implements EventListener {

        private Tag tag;
        private String objectName;

        public AutoTextboxSaveObject(Tag tag, String objectName) {
            this.tag = tag;
            this.objectName = objectName;
        }

        @Override
        public void onEvent(Event t) throws Exception {
            Textbox txt = (Textbox) t.getTarget();
            PropertyUtils.setProperty(tag, objectName, txt.getValue());
        }
    }

    private static class AutoTextboxSaveObject2 implements EventListener {

        private Tag tag;

        public AutoTextboxSaveObject2(Tag tag) {
            this.tag = tag;
        }

        @Override
        public void onEvent(Event t) throws Exception {
            Textbox txt = (Textbox) t.getTarget();
            tag.setUsername(txt.getValue());
        }
    }

    private static class AutoComboboxSaveObject implements EventListener {

        private Tag tag;
        private String objectName;

        public AutoComboboxSaveObject(Tag tag, String objectName) {
            this.tag = tag;
            this.objectName = objectName;
        }

        @Override
        public void onEvent(Event t) throws Exception {
            Combobox cbb = (Combobox) t.getTarget();
            PropertyUtils.setProperty(tag, objectName, cbb.getSelectedItem().getValue());
        }
    }

    private Combobox buildModeCombobox(String defaultValue) {
        Combobox cbb = new Combobox();

        Comboitem item1 = new Comboitem();
        Comboitem item2 = new Comboitem();
        Comboitem item3 = new Comboitem();

        item1.setLabel("MODIFY");
        item1.setValue("MODIFY");
        cbb.appendChild(item1);

        item2.setLabel("DEL");
        item2.setValue("DEL");
        cbb.appendChild(item2);

        item3.setLabel("ADD");
        item3.setValue("ADD");
        cbb.appendChild(item3);

        cbb.setSelectedItem(item1);

        if (defaultValue != null) {
            if (defaultValue.equals("MODIFY")) {
                cbb.setSelectedItem(item1);
            } else if (defaultValue.equals("DEL")) {
                cbb.setSelectedItem(item2);
            } else if (defaultValue.equals("ADD")) {
                cbb.setSelectedItem(item3);
            }
        }

        return cbb;
    }
}
